﻿using UnityEngine;
using System.Collections;

public static class VectorExtension {

    public static Vector3 RotateX(this Vector3 v, float angle) {
        angle *= Mathf.Deg2Rad; 
        float sin = Mathf.Sin(angle);
        float cos = Mathf.Cos(angle);
        float y = (cos * v.y) - (sin * v.z);
        float z = (cos * v.z) + (sin * v.y);
        return new Vector3(v.x, y, z);
    }

    public static Vector3 RotateY(this Vector3 v, float angle) {
        angle *= Mathf.Deg2Rad;
        float sin = Mathf.Sin(angle);
        float cos = Mathf.Cos(angle);
        float x = (cos * v.x) + (sin * v.z);
        float z = (cos * v.z) - (sin * v.x);
        return new Vector3(x, v.y, z);
    }

    public static Vector3 RotateZ(this Vector3 v, float angle) {
        angle *= Mathf.Deg2Rad;
        float sin = Mathf.Sin(angle);
        float cos = Mathf.Cos(angle);
        float x = (cos * v.x) - (sin * v.y);
        float y = (cos * v.y) + (sin * v.x);
        return new Vector3(x, y, v.z);
    }

    public static Vector2 Rotate(this Vector2 v, float angle) {
        angle *= Mathf.Deg2Rad;
        float sin = Mathf.Sin(angle);
        float cos = Mathf.Cos(angle);
        float x = (cos * v.x) - (sin * v.y);
        float y = (cos * v.y) + (sin * v.x);
        return new Vector2(x, y);
    }

    public static Vector3 NewX(this Vector3 v, float x) { return new Vector3(x, v.y, v.z); }
    public static Vector3 NewY(this Vector3 v, float y) { return new Vector3(v.x, y, v.z); }
    public static Vector3 NewZ(this Vector3 v, float z) { return new Vector3(v.x, v.y, z); }
    public static Vector2 NewX(this Vector2 v, float x) { return new Vector3(x, v.y); }
    public static Vector2 NewY(this Vector2 v, float y) { return new Vector3(v.x, y); }
    public static Vector3 ZeroZ(this Vector3 v) { return v.NewZ(0); }
}
